﻿package view.popup
{
    import com.sh.game.consts.*;
    import com.sh.game.global.*;
    import com.sh.game.util.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.text.*;
    import manager.*;
    import morn.core.components.*;
    import plugin.bagN.*;

    public class NewExplodeDialogUI extends View implements IDialogUI
    {
        public var closeBtn:Button;
        public var sureBtn:Button;
        public var btnLeft:Button;
        public var btnRight:Button;
        protected var uiXML:XML;
        private var nametext:TextField;
        private var amounttext:TextField;
        private var curCount:int = 1;
        private var data:Object;
        private var item:Loader;
        private var max:int;
        private var background:Sprite;

        public function NewExplodeDialogUI()
        {
            this.uiXML = <View>r
n	t	t	t  <Image url=""png.common.cs2_tsbg"" x=""-1"" y=""-1"" width=""353"" height=""291"" sizeGrid=""10,50,10,50""/>r
n	t	t	t  <Button skin=""png.common.btn_cs2_close"" x=""318"" y=""3"" var=""closeBtn""/>r
n	t	t	t  <Image url=""png.common.cs2_di2nei"" x=""6"" y=""34"" width=""340"" height=""247"" sizeGrid=""10,10,10,10""/>r
n	t	t	t  <Image url=""png.common.img_2"" x=""38"" y=""70""/>r
n	t	t	t  <Button skin=""png.common.btn_cs2anniu"" x=""120"" y=""228"" var=""sureBtn""/>r
n	t	t	t  <Image url=""png.common.s9_frame_down"" x=""146"" y=""151"" width=""80"" height=""25"" sizeGrid=""5,16,5,29""/>r
n	t	t	t  <Button skin=""png.common.btn_zuo"" x=""125"" y=""152"" var=""btnLeft""/>r
n	t	t	t  <Button skin=""png.common.btn_jiantou"" x=""236"" y=""152"" var=""btnRight""/>r
n	t	t	t  <Label text=""拆分物品"" x=""146"" y=""4"" color=""0xffe4c7"" font=""Microsoft YaHei"" size=""15"" bold=""true""/>r
n	t	t	t</View>")("<View>
			  <Image url="png.common.cs2_tsbg" x="-1" y="-1" width="353" height="291" sizeGrid="10,50,10,50"/>
			  <Button skin="png.common.btn_cs2_close" x="318" y="3" var="closeBtn"/>
			  <Image url="png.common.cs2_di2nei" x="6" y="34" width="340" height="247" sizeGrid="10,10,10,10"/>
			  <Image url="png.common.img_2" x="38" y="70"/>
			  <Button skin="png.common.btn_cs2anniu" x="120" y="228" var="sureBtn"/>
			  <Image url="png.common.s9_frame_down" x="146" y="151" width="80" height="25" sizeGrid="5,16,5,29"/>
			  <Button skin="png.common.btn_zuo" x="125" y="152" var="btnLeft"/>
			  <Button skin="png.common.btn_jiantou" x="236" y="152" var="btnRight"/>
			  <Label text="拆分物品" x="146" y="4" color="0xffe4c7" font="Microsoft YaHei" size="15" bold="true"/>
			</View>;
            return;
        }// end function

        override protected function createChildren() : void
        {
            createView(this.uiXML);
            this.init();
            return;
        }// end function

        override protected function initialize() : void
        {
            super.initialize();
            return;
        }// end function

        public function init() : void
        {
            this.background = new Sprite();
            this.background.graphics.beginFill(0, 0.5);
            this.background.graphics.drawRect(0, 0, GlobalLayer.instance.root.stage.stageWidth, GlobalLayer.instance.root.stage.stageHeight);
            this.background.graphics.endFill();
            this.background.addEventListener(MouseEvent.MOUSE_DOWN, this.onDown);
            PluginManager.instance.alertContainer.addChild(this.background);
            var _loc_2:Boolean = false;
            this.visible = false;
            this.background.visible = _loc_2;
            this.x = GlobalLayer.instance.root.stage.stageWidth - this.width >> 1;
            this.y = GlobalLayer.instance.root.stage.stageHeight - this.height >> 1;
            this.nametext = new TextField();
            this.nametext.x = 100;
            this.nametext.y = 84;
            this.nametext.width = 100;
            this.nametext.height = 22;
            this.nametext.defaultTextFormat = new TextFormat("宋体", 12);
            this.nametext.textColor = MColor.T_WHITE;
            this.nametext.autoSize = TextFieldAutoSize.CENTER;
            this.addChild(this.nametext);
            var _loc_1:* = new TextField();
            _loc_1.x = 62;
            _loc_1.y = 153;
            _loc_1.width = 60;
            _loc_1.height = 20;
            _loc_1.defaultTextFormat = new TextFormat("宋体", 12);
            _loc_1.text = "拆分数量";
            _loc_1.textColor = MColor.T_WHITE;
            _loc_1.autoSize = TextFieldAutoSize.LEFT;
            this.addChild(_loc_1);
            this.amounttext = new TextField();
            this.amounttext.x = 146;
            this.amounttext.y = 153;
            this.amounttext.width = 80;
            this.amounttext.height = 20;
            this.amounttext.defaultTextFormat = new TextFormat("宋体", 12);
            this.amounttext.text = String(this.curCount);
            this.amounttext.textColor = MColor.V_NORMAL;
            this.amounttext.autoSize = TextFieldAutoSize.CENTER;
            this.amounttext.type = TextFieldType.INPUT;
            this.amounttext.addEventListener(MouseEvent.CLICK, this.onInput);
            this.addChild(this.amounttext);
            this.btnLeft.addEventListener(MouseEvent.CLICK, this.onLeft);
            this.btnRight.addEventListener(MouseEvent.CLICK, this.onRight);
            this.sureBtn.label = "确认";
            this.sureBtn.labelColors = "0xb29a6c, 0xb29a6c, 0xb29a6c";
            this.sureBtn.labelFont = "宋体";
            this.sureBtn.labelStroke = "1";
            this.sureBtn.addEventListener(MouseEvent.CLICK, this.onSure);
            this.closeBtn.addEventListener(MouseEvent.CLICK, this.onClose);
            this.addEventListener(MouseEvent.CLICK, this.onStageClicked);
            this.addEventListener(MouseEvent.MOUSE_DOWN, this.onDown);
            GlobalLayer.instance.root.addEventListener(Event.RESIZE, this.resizeHandler);
            return;
        }// end function

        private function onInput(event:MouseEvent) : void
        {
            GlobalLayer.instance.flashStage.focus = this.amounttext;
            event.stopImmediatePropagation();
            this.curCount = int(this.amounttext.text);
            this.changeNumText(this.curCount);
            return;
        }// end function

        private function onLeft(event:MouseEvent) : void
        {
            this.curCount = 1;
            this.changeNumText(this.curCount);
            return;
        }// end function

        private function onRight(event:MouseEvent) : void
        {
            this.curCount = this.max;
            this.changeNumText(this.curCount);
            return;
        }// end function

        private function changeNumText(param1:int) : void
        {
            if (param1 > this.max)
            {
                param1 = this.max;
            }
            this.amounttext.text = String(param1);
            return;
        }// end function

        private function onStageClicked(event:MouseEvent) : void
        {
            GlobalLayer.instance.flashStage.focus = null;
            this.curCount = int(this.amounttext.text);
            this.changeNumText(this.curCount);
            return;
        }// end function

        private function onSure(event:MouseEvent) : void
        {
            if (this.curCount > 0)
            {
                PluginManager.instance.sendToServer("bag", CMDBag.ExplodeItem, {index:this.data.index, count:int(this.amounttext.text)});
            }
            this.close();
            return;
        }// end function

        public function show(param1:Object) : void
        {
            this.data = param1;
            if (this.data == null || this.data[4] == null)
            {
                return;
            }
            this.drawItem();
            this.max = this.data[1] - 1;
            var _loc_2:int = 1;
            if (this.max == 0)
            {
                _loc_2 = 0;
            }
            this.nametext.text = this.data[4].name;
            var _loc_3:Boolean = true;
            this.visible = true;
            this.background.visible = _loc_3;
            return;
        }// end function

        public function onShow(param1:Object) : void
        {
            return;
        }// end function

        private function drawItem() : void
        {
            if (this.item)
            {
                this.item.parent.removeChild(this.item);
            }
            var imgURL:* = new URLRequest();
            imgURL.url = Config.getUrl("assets/images/icon/tools/" + this.data[4].icon + ".png", ResourceType.NORMAL);
            this.item = new Loader();
            this.item.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function () : void
            {
                return;
            }// end function
            );
            this.item.load(imgURL);
            this.item.x = 41;
            this.item.y = 76;
            this.addChild(this.item);
            return;
        }// end function

        private function onClose(event:MouseEvent) : void
        {
            this.close();
            return;
        }// end function

        public function close() : void
        {
            var _loc_1:Boolean = false;
            this.visible = false;
            this.background.visible = _loc_1;
            this.data = null;
            return;
        }// end function

        private function onDown(event:MouseEvent) : void
        {
            event.stopImmediatePropagation();
            return;
        }// end function

        private function resizeHandler(event:Event) : void
        {
            this.background.width = GlobalLayer.instance.root.stage.stageWidth;
            this.background.height = GlobalLayer.instance.root.stage.stageHeight;
            this.x = GlobalLayer.instance.root.stage.stageWidth - this.width >> 1;
            this.y = GlobalLayer.instance.root.stage.stageHeight - this.height >> 1;
            return;
        }// end function

    }
}
